package com.UnionPayCenter.ali.manager.impl;

import com.UnionPayCenter.ali.manager.AliManager;
import com.UnionPayCenter.ali.util.AliUtils;
import com.alipay.api.AlipayApiException;
import com.alipay.api.request.AlipayOpenAuthTokenAppRequest;
import com.alipay.api.request.AlipaySystemOauthTokenRequest;
import com.alipay.api.request.AlipayUserInfoShareRequest;
import com.alipay.api.response.AlipaySystemOauthTokenResponse;
import com.alipay.api.response.AlipayUserInfoShareResponse;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;


/**
 * @author noah
 * @since 1.0.0, 2019/09/09
 */
@Service
public class AliManagerImpl implements AliManager {



    @Override
    public AlipaySystemOauthTokenResponse outhToken(String authCode) throws AlipayApiException {
        AlipaySystemOauthTokenRequest request = new AlipaySystemOauthTokenRequest();
        if(StringUtils.isNotBlank(authCode)){
            request.setGrantType("authorization_code");
            request.setCode(authCode);
            request.setRefreshToken("");
            request.setProdCode("2019082566441335");
        }else {
            request.setGrantType("refresh_token");
            request.setCode("");
            request.setRefreshToken("201909BB72520493a26e43cf95be1e45b507aX02");
        }
        AlipaySystemOauthTokenResponse response = AliUtils.getCertAlipayRequest().certificateExecute(request);
        if(response.isSuccess()){
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
        }
        return response;
    }

    @Override
    public AlipayUserInfoShareResponse getAliUserInfo(String accessToken) throws AlipayApiException {
        AlipayUserInfoShareRequest request = new AlipayUserInfoShareRequest();
        AlipayUserInfoShareResponse response = AliUtils.getSecKeyAlipayRequest().certificateExecute(request, accessToken);
        if(response.isSuccess()){
            System.out.println("获取会员信息 - 调用成功");
            return response;
        }
        return null;
    }
}
